// 12.7 切换 mall_id 的方法
import apiUrl from '../../core/apiUrl.js';
import applunch from '../../core/appOnLaunch.js';
// import mallId from './malid.js';
import siteInfo from 'siteInfo';

let apiRoot = '';


const state = {
	mall_id: 1,
	apimaUrl: []
};

const getters = {
	getMallId(state) {
		return state.mall_id;
	}
};

const mutations = {
	mutSetMallId(state, data) {
		state.mall_id = data;
	}
};

const actions = {
	actionSetMallId(context, data) {
		if (siteInfo.acid > 0) {
			const scriptUrl = siteInfo.siteroot.substr(0, siteInfo.siteroot.indexOf('app/index.php')) +
				'addons/zjhj_bd/web/index.php';
			apiRoot = scriptUrl + '?_acid=' + siteInfo.acid + '&r=';
		} else {
			apiRoot = siteInfo.apiroot + '?_mall_id=' + data + '&r=';
		}
		let newUrl = {};
		for (let k in apiUrl) {
			let newOb = {
				[k]: {}
			};
			for (let i in apiUrl[k]) {
				newOb[k][i] = `${apiRoot}${apiUrl[k][i]}`
			}
			newUrl[k] = newOb[k];
		}
		state.apimaUrl = newUrl
		context.commit('mutSetMallId', data);
	}
};

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
